﻿var Order;
var Address;
var totalGoodsMoney = 0.00;
var sendFee = 0.00;
var totalWeight = 0;


///检查表单；
function CheckForm() {
    if ($("#txtReceiverName").val() == "") {
        alert("系统提示：请输入收货人！");
        $("#txtReceiverName").focus();
        return false;
    }
    if ($("#ddlProvince").val() == "") {
        alert("系统提示：请选择所在省份！");
        $("#ddlProvince").focus();
        return false;
    }
    if ($("#ddlCity").val() == "") {
        alert("系统提示：请选择所在城市！");
        $("#ddlCity").focus();
        return false;
    }
    if ($("#ddlArea").val() == "") {
        alert("系统提示：请选择所在区域！");
        $("#ddlArea").focus();
        return false;
    }
    if ($("#txtReceiverAddress").val() == "") {
        alert("系统提示：请输入详细地址！");
        $("#txtReceiverAddress").focus();
        return false;
    }
    if ($("#txtReceiverMobile").val() == "" && $("#txtReceiverPhone").val() == "") {
        alert("系统提示：手机号码与固定电话必须填一项！！");
        $("#txtReceiverMobile").focus();
        return false;
    }
    return true;


}


///切换收货地址。
function ChangeAddress(bianhao) {
    var curAddress = Address[bianhao];
   
    $("#txtReceiverName").val(curAddress.AddressInfo._receivername);
    $("#ddlProvince").val(curAddress.Area.Province._id);
    QueryCities(curAddress.Area.Province._id, 'ddlCity', curAddress.Area.Area._cityid);
    QueryAreas(curAddress.Area.Area._cityid, 'ddlArea', curAddress.AddressInfo._areaid);
    $("#txtReceiverAddress").val(curAddress.AddressInfo._receiveraddress);
    $("#txtReceiverMobile").val(curAddress.AddressInfo._receivermobile);
    $("#txtReceiverZipCode").val(curAddress.AddressInfo._receiverzipcode);
}

//计算运费。
function ComputeSendFee() {
   
    //更新服务器端数据；
    var url = "/home/as/OrderInfoCommon.aspx?Action=computesendfee&deliveryID=" + getRadioValue("rbSendMode");
    url += "&totalWeight=" + totalWeight;
    AjaxGetEx(url, "", function (ret) {
        if (ret.Result == 1) {
            sendFee = parseFloat(ret.Message);
            UpdateMoney();
        }
        else {
            alert("提示：" + ret.Message);
        }
    }, true);
}

function UpdateMoney() {
  
    var couponMoney = 0;
    if (getRadioValue("rbCoupon") != "")
        couponMoney = parseFloat($("#hfCoupon" + getRadioValue("rbCoupon")).val());

    var totalMoney = totalGoodsMoney.Sub(couponMoney).Add(sendFee);
    if (totalMoney < 0) totalMoney = 0;
   
    $("#spSendFee").html(formatnumber(sendFee, 2));
    $("#spCouponMoney").html(formatnumber(couponMoney, 2));
    $("#spOrderMoney").html(formatnumber(totalMoney, 2));
}


//保存订单；
function SaveOrder() {
    if (!CheckForm())
        return;

    if (!confirm("提示：确认要提交订单？"))
        return;
    Order.Order._orderdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    Order.Order._sendmode = $("#hfSendMode" + getRadioValue("rbSendMode")).val();
    Order.Order._sendfee = sendFee;
    Order.Order._paymode = getRadioValue("rbPayMode");
    Order.Order._couponsn = null;
    Order.Order._couponmoney = 0;
    if (getRadioValue("rbCoupon") != "") {
        Order.Order._couponsn = getRadioValue("rbCoupon");
        Order.Order._couponmoney = $("#hfCoupon" + getRadioValue("rbCoupon")).val();
    }

    Order.Order._receivername = $("#txtReceiverName").val();
    Order.Order._areaid = $("#ddlArea").val();
    Order.Order._receiveraddress = $("#txtReceiverAddress").val();
    Order.Order._receiverzipcode = $("#txtReceiverZipCode").val();
    Order.Order._receivermobile = $("#txtReceiverMobile").val();
    Order.Order._receiverphone = $("#txtReceiverPhone").val();
    Order.Order._remark = $("#txtRemark").val();
    Order.Order._fapiao = $("#txtFaPiao").val();
    var UserAddressID = 0;
    if (getRadioValue("rbAddress") != "")
        UserAddressID = Address[getRadioValue("rbAddress")].AddressInfo._useraddressid;
    var url = "/Home/As/OrderInfoCommon.aspx?action=saveorder";
    var postData = "jsonData=" + JSON.stringify(eval(Order));
    postData += "&UserAddressID=" + UserAddressID;
    AjaxPostEx2(url, "", postData, function (ret) {
        if (ret.Result == 1) {
            alert("提示：您的订单已保存成功！");
            location.href = '/Home/Pay/Alipay.aspx?orderid='+ret.Data;
            //location.href = '/Home/MyOrders.aspx';
        }
        else {
            alert("提示：" + ret.Message);
        }

    }
    );

}
